package com.gitee.ixtf.vertx.internal.converter

import com.gitee.ixtf.vertx.ParameterConverter
import com.gitee.ixtf.vertx.internal.rc
import graphql.schema.DataFetchingEnvironment
import io.vertx.core.eventbus.Message
import io.vertx.ext.web.RoutingContext
import jakarta.ws.rs.PathParam
import java.lang.reflect.Parameter

class PathParamParameterConverter(
    private val parameter: Parameter,
) : ParameterConverter<Any> {
  private val key by lazy { parameter.getAnnotation(PathParam::class.java).value }
  private val converter by lazy { implicitConverter(parameter) }

  override suspend fun convert(env: DataFetchingEnvironment) = convert(env.rc())

  override suspend fun convert(rc: RoutingContext): Any = converter.convert(rc.pathParam(key))

  override suspend fun convert(message: Message<*>): Any = converter.convert(message.headers()[key])
}
